<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JavaScript <span class="color_h1">类型转换</span></h1>

<hr>
<p>Number() 转换为数字， String() 转换为字符串， Boolean() 转换为布尔值。 </p>
<hr>
<h2>JavaScript 数据类型</h2>
<p>在 JavaScript 中有 6 种不同的数据类型：</p>
<ul>
	<li>string</li>
	<li>number</li>
	<li>boolean</li>
	<li>object</li>
	<li>function</li>
<li>symbol</li>
</ul>
<p>3 种对象类型：</p>
<ul>
	<li>Object</li>
	<li>Date</li>
	<li>Array</li>
</ul>
<p> 2 个不包含任何值的数据类型：</p>
<ul>
	<li>null</li>
	<li>undefined</li>
</ul>
<hr>

<h2>typeof 操作符</h2>
<p>你可以使用 <strong>typeof</strong> 操作符来查看 JavaScript 变量的数据类型。</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<span class="highELE">typeof</span> <span class="highVAL">"John"</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<span class="highCOM">// 返回 string <br></span>
<span class="highELE">typeof</span> <span class="highVAL">3.14</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<span class="highCOM">// 返回 number<br></span>
<span class="highELE">typeof</span> NaN &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<span class="highCOM">// 返回 number<br></span><span class="highELE">typeof</span> <span class="highVAL">false</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 boolean<br></span><span class="highELE">typeof</span> [<span class="highVAL">1</span>,<span class="highVAL">2</span>,<span class="highVAL">3</span>,<span class="highVAL">4</span>]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="highCOM">// 返回 object<br></span>
<span class="highELE">typeof</span> {name:<span class="highVAL">'John'</span>, age:34}&nbsp;
<span class="highCOM">// 返回 object<br></span><span class="highELE">typeof</span> <span class="highELE">new</span> Date()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<span class="highCOM">// 返回 object<br></span><span class="highELE">typeof</span> <span class="highELE">function</span> () {} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 function<br></span>
<span class="highELE">typeof</span> myCar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<span class="highCOM">// 返回 undefined (如果 myCar 没有声明)<br></span>
<span class="highELE">typeof</span> <span class="highVAL">null</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<span class="highCOM">// 返回 object
</span><button class="copy-code-button" type="button" data-clipboard-text="typeof &quot;John&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// 返回 string 
typeof 3.14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// 返回 number
typeof NaN &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// 返回 numbertypeof false&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 返回 booleantypeof [1,2,3,4]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 返回 object
typeof {name:'John', age:34}&nbsp;
// 返回 objecttypeof new Date()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// 返回 objecttypeof function () {} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 返回 function
typeof myCar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// 返回 undefined (如果 myCar 没有声明)
typeof null&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// 返回 object"></button></div>
<br>
<a class="tryitbtn" href="/try/tryit.php?filename=tryjs_typeof_all" target="_blank" rel="noopener noreferrer">尝试一下 »</a>
</div>
<p>请注意：</p>
<ul>
	<li> NaN 的数据类型是 number</li>
	<li> 数组(Array)的数据类型是 object</li>
	<li> 日期(Date)的数据类型为 object</li>
	<li> null 的数据类型是 object</li>
	<li> 未定义变量的数据类型为 undefined</li>
</ul>

<p>如果对象是 JavaScript Array 或 JavaScript Date ，我们就无法通过 <strong>typeof</strong> 来判断他们的类型，因为都是 返回 object。</p>
<hr>

<h2>constructor 属性</h2>
<p> <strong>constructor</strong> 属性返回所有 JavaScript 变量的构造函数。</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<span class="highVAL">"John"</span>.constructor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<span class="highCOM">// 返回函数 String()&nbsp; { [native code] }<br></span>
(<span class="highVAL">3.14</span>).constructor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<span class="highCOM">// 返回函数 Number()&nbsp; { [native code] }<br></span>
false.constructor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<span class="highCOM">// 返回函数 Boolean() { [native code] }<br></span>
[<span class="highVAL">1</span>,<span class="highVAL">2</span>,<span class="highVAL">3</span>,<span class="highVAL">4</span>].constructor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<span class="highCOM">// 返回函数 Array()&nbsp;&nbsp; { [native code] }<br></span>
{name:<span class="highVAL">'John'</span>, age:34}.constructor&nbsp;
<span class="highCOM">// 返回函数 Object()&nbsp; { [native code] }<br></span>
	<span class="highELE">new</span> Date().constructor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
<span class="highCOM">// 返回函数 Date()&nbsp; &nbsp; { [native code] }<br></span>
<span class="highELE">function</span> () {}.constructor &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<span class="highCOM">// 返回函数 Function(){ [native code] }
</span><button class="copy-code-button" type="button" data-clipboard-text="&quot;John&quot;.constructor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// 返回函数 String()&nbsp; { [native code] }
(3.14).constructor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// 返回函数 Number()&nbsp; { [native code] }
false.constructor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// 返回函数 Boolean() { [native code] }
[1,2,3,4].constructor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// 返回函数 Array()&nbsp;&nbsp; { [native code] }
{name:'John', age:34}.constructor&nbsp;
// 返回函数 Object()&nbsp; { [native code] }
	new Date().constructor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
// 返回函数 Date()&nbsp; &nbsp; { [native code] }
function () {}.constructor &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// 返回函数 Function(){ [native code] }"></button></div>
<br>
<a class="tryitbtn" href="/try/tryit.php?filename=tryjs_constructor_all" target="_blank" rel="noopener noreferrer">尝试一下 »</a>
</div>

<p>
你可以使用 constructor 属性来查看对象是否为数组
(包含字符串 "Array"):</p>

<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<span class="highELE">function</span> isArray(myArray) {<br>
&nbsp;&nbsp;&nbsp; <span class="highELE">return</span> myArray.constructor.toString().indexOf(<span class="highVAL">"Array"</span>) &gt; <span class="highVAL">-1</span>;<br>
}
<button class="copy-code-button" type="button" data-clipboard-text="function isArray(myArray) {
&nbsp;&nbsp;&nbsp; return myArray.constructor.toString().indexOf(&quot;Array&quot;) > -1;
}"></button></div>
<br>
	<a class="tryitbtn" href="/try/tryit.php?filename=tryjs_array_isarray" target="_blank" rel="noopener noreferrer">尝试一下 »</a>
</div>

<p>你可以使用 constructor 属性来查看对象是否为日期
(包含字符串 "Date"):</p>

<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<span class="highELE">function</span> isDate(myDate) {<br>
&nbsp;&nbsp;&nbsp; <span class="highELE">return</span> myDate.constructor.toString().indexOf(<span class="highVAL">"Date"</span>) &gt; <span class="highVAL">-1</span>;<br>
}
<button class="copy-code-button" type="button" data-clipboard-text="function isDate(myDate) {
&nbsp;&nbsp;&nbsp; return myDate.constructor.toString().indexOf(&quot;Date&quot;) > -1;
}"></button></div>
<br>
	<a class="tryitbtn" href="/try/tryit.php?filename=tryjs_date_isdate" target="_blank" rel="noopener noreferrer">尝试一下 »</a>
</div>
<br>
<hr>

<h2>JavaScript 类型转换</h2>
<p>JavaScript 变量可以转换为新变量或其他数据类型：</p>
<ul>
	<li>通过使用 JavaScript 函数</li>
	<li>通过 JavaScript 自身自动转换</li>
</ul>
<hr>
<h2>将数字转换为字符串</h2>
<p>全局方法 <strong>String()</strong> 可以将数字转换为字符串。</p>

<p>该方法可用于任何类型的数字，字母，变量，表达式：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
	String(x)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="highCOM">// 将变量 x 转换为字符串并返回<br></span>
	String(<span class="highVAL">123</span>)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="highCOM">// 将数字 123 转换为字符串并返回<br></span>
	String(<span class="highVAL">100</span> + <span class="highVAL">23</span>)&nbsp; <span class="highCOM">// 将数字表达式转换为字符串并返回</span><button class="copy-code-button" type="button" data-clipboard-text="String(x)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 将变量 x 转换为字符串并返回
	String(123)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // 将数字 123 转换为字符串并返回
	String(100 + 23)&nbsp; // 将数字表达式转换为字符串并返回"></button></div>
<br>
	<a class="tryitbtn" href="/try/tryit.php?filename=tryjs_number_string" target="_blank" rel="noopener noreferrer">尝试一下 »</a>
</div>
<p> Number 方法 <strong>toString()</strong> 也是有同样的效果。</p>

<div class="example">
<h2 class="example">实例</h2>
	<div class="example_code">
x.toString()<br>(<span class="highVAL">123</span>).toString()<br>(<span class="highVAL">100</span> + <span class="highVAL">23</span>).toString()<button class="copy-code-button" type="button" data-clipboard-text="x.toString()(123).toString()(100 + 23).toString()"></button></div>
<br>
<a class="tryitbtn" href="/try/tryit.php?filename=tryjs_number_tostring" target="_blank" rel="noopener noreferrer">尝试一下 »</a>
</div>
<p>在 <a href="/jsref/jsref-obj-number.html">Number 方法</a> 章节中，你可以找到更多数字转换为字符串的方法：</p>
<table class="reference">
  <tbody><tr>
    <th style="width:25%">方法</th>
    <th>描述</th>
  </tr>
  <tr>
    <td>toExponential()</td>
    <td>把对象的值转换为指数计数法。</td>
  </tr>
	<tr>
    <td>toFixed()</td>
    <td>把数字转换为字符串，结果的小数点后有指定位数的数字。</td>
  </tr>
  <tr>
    <td>toPrecision()</td>
    <td>把数字格式化为指定的长度。</td>
  </tr>
</tbody></table>
<br>
<hr>

<h2>将布尔值转换为字符串</h2>

<p>全局方法 <strong>String()</strong> 可以将布尔值转换为字符串。 </p>

<div class="example">
<div class="example_code">
String(<span class="highVAL">false</span>)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 "false"<br></span>
String(<span class="highVAL">true</span>)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span class="highCOM">// 返回 "true"
</span><button class="copy-code-button" type="button" data-clipboard-text="String(false)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; // 返回 &quot;false&quot;
String(true)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; // 返回 &quot;true&quot;"></button></div>
</div>

<p>Boolean 方法 <strong>toString()</strong> 也有相同的效果。</p>

<div class="example">
<div class="example_code">
false.toString()&nbsp;&nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 "false"<br></span>
true.toString()&nbsp; &nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 "true"
</span><button class="copy-code-button" type="button" data-clipboard-text="false.toString()&nbsp;&nbsp;&nbsp;&nbsp; // 返回 &quot;false&quot;
true.toString()&nbsp; &nbsp;&nbsp;&nbsp; // 返回 &quot;true&quot;"></button></div>
</div>
<br>
<hr>

<h2>将日期转换为字符串</h2>
<p>Date() 返回字符串。</p>
<div class="example">
<div class="example_code">
	Date()&nbsp;&nbsp;&nbsp; &nbsp; <span class="highCOM">// 返回 Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)</span><button class="copy-code-button" type="button" data-clipboard-text="Date()&nbsp;&nbsp;&nbsp; &nbsp; // 返回 Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"></button></div>
</div>
<p>
全局方法 String() 可以将日期对象转换为字符串。</p>
<div class="example">
<div class="example_code">
	String(new Date())&nbsp;&nbsp;&nbsp; &nbsp; <span class="highCOM">// 返回 Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)</span><button class="copy-code-button" type="button" data-clipboard-text="String(new Date())&nbsp;&nbsp;&nbsp; &nbsp; // 返回 Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"></button></div>
</div>

<p> Date 方法 <strong>toString()</strong> 也有相同的效果。</p>

<div class="example">
<h2 class="example">实例</h2>
	<div class="example_code">obj = new Date()<br>
		obj.toString()&nbsp;&nbsp; <span class="highCOM">// 返回 Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)</span><button class="copy-code-button" type="button" data-clipboard-text="obj = new Date()
		obj.toString()&nbsp;&nbsp; // 返回 Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)"></button></div>
</div>

<p>在 <a href="/jsref/jsref-obj-date.html">Date 方法</a> 章节中，你可以查看更多关于日期转换为字符串的函数：</p>
<table class="reference">
<tbody><tr>
<th>方法</th>
<th>描述</th>
</tr>
<tr>
<td>getDate()</td>
<td>从 Date 对象返回一个月中的某一天 (1 ~ 31)。</td>
</tr>
<tr>
<td>getDay()</td>
<td>从 Date 对象返回一周中的某一天 (0 ~ 6)。</td>
</tr>
<tr>
<td>getFullYear()</td>
<td>从 Date 对象以四位数字返回年份。</td>
</tr>
<tr>
<td>getHours()</td>
<td>返回 Date 对象的小时 (0 ~ 23)。</td>
</tr>
<tr>
<td>getMilliseconds()</td>
<td>返回 Date 对象的毫秒(0 ~ 999)。</td>
</tr>
<tr>
<td>getMinutes()</td>
<td>返回 Date 对象的分钟 (0 ~ 59)。</td>
</tr>
<tr>
<td>getMonth()</td>
<td>从 Date 对象返回月份 (0 ~ 11)。</td>
</tr>
<tr>
<td>getSeconds()</td>
<td>返回 Date 对象的秒数 (0 ~ 59)。</td>
</tr>
<tr>
<td>getTime()</td>
<td>返回 1970 年 1 月 1 日至今的毫秒数。</td>
</tr>
</tbody></table>
<br>

<hr>
<h2>将字符串转换为数字</h2>
<p>全局方法 <strong>Number()</strong> 可以将字符串转换为数字。</p>
<p>字符串包含数字(如 "3.14") 转换为数字 (如 3.14).</p>
<p>空字符串转换为 0。</p>
<p>其他的字符串会转换为 NaN (不是个数字)。</p>

<div class="example">
<div class="example_code">
Number(<span class="highVAL">"3.14"</span>)&nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 3.14<br></span>
Number(<span class="highVAL">" "</span>)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 0 <br></span>
Number(<span class="highVAL">""</span>)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 	<span class="highCOM">// 返回 0<br></span>
Number(<span class="highVAL">"99 88"</span>)&nbsp;&nbsp; <span class="highCOM">// 返回 NaN
</span><button class="copy-code-button" type="button" data-clipboard-text="Number(&quot;3.14&quot;)&nbsp;&nbsp;&nbsp; // 返回 3.14
Number(&quot; &quot;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 返回 0 
Number(&quot;&quot;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 	// 返回 0
Number(&quot;99 88&quot;)&nbsp;&nbsp; // 返回 NaN"></button></div>
</div>
<p>在 <a href="/jsref/jsref-obj-number.html">Number 方法</a> 章节中，你可以查看到更多关于字符串转为数字的方法：</p>
<table class="reference">
  <tbody><tr>
    <th style="width:25%">方法</th>
    <th>描述</th>
  </tr>
  <tr>
    <td>parseFloat()</td>
    <td>解析一个字符串，并返回一个浮点数。</td>
    </tr>
  <tr>
    <td>parseInt()</td>
    <td>解析一个字符串，并返回一个整数。</td>
    </tr>
</tbody></table>
<br>

<hr>

<h2>一元运算符 + </h2>

<p> <strong>Operator + </strong> 可用于将变量转换为数字：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<span class="highELE">var</span> y = <span class="highVAL">"5"</span>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<span class="highCOM">// y 是一个字符串<br></span>
<span class="highELE">var</span> x = + y;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<span class="highCOM">// x 是一个数字
</span><button class="copy-code-button" type="button" data-clipboard-text="var y = &quot;5&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// y 是一个字符串
var x = + y;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// x 是一个数字"></button></div>
<br>
<a class="tryitbtn" href="/try/tryit.php?filename=tryjs_typeof3" target="_blank" rel="noopener noreferrer">尝试一下 »</a>
</div>
<p>如果变量不能转换，它仍然会是一个数字，但值为 NaN 
(不是一个数字):</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<span class="highELE">var</span> y = <span class="highVAL">"John"</span>;&nbsp;&nbsp;
<span class="highCOM">// y 是一个字符串<br></span>
<span class="highELE">var</span> x = + y;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="highCOM">// x 是一个数字 (NaN)</span><button class="copy-code-button" type="button" data-clipboard-text="var y = &quot;John&quot;;&nbsp;&nbsp;
// y 是一个字符串
var x = + y;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// x 是一个数字 (NaN)"></button></div>
<br>
	<a class="tryitbtn" href="/try/tryit.php?filename=tryjs_typeof4" target="_blank" rel="noopener noreferrer">尝试一下 »</a>
</div>
<br>

<hr>
<h2>将布尔值转换为数字</h2>
<p>全局方法 <strong>Number()</strong> 可将布尔值转换为数字。</p>

<div class="example">
<div class="example_code">
Number(<span class="highVAL">false</span>)&nbsp;&nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 0<br></span>
Number(<span class="highVAL">true</span>)&nbsp; &nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 1
</span><button class="copy-code-button" type="button" data-clipboard-text="Number(false)&nbsp;&nbsp;&nbsp;&nbsp; // 返回 0
Number(true)&nbsp; &nbsp;&nbsp;&nbsp; // 返回 1"></button></div>
</div>
<br>
<hr>

<h2>将日期转换为数字</h2>

<p>全局方法 <strong>Number()</strong> 可将日期转换为数字。</p>

<div class="example">
<div class="example_code">
d = <span class="highELE">new</span> Date();<br>Number(d)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 1404568027739
</span><button class="copy-code-button" type="button" data-clipboard-text="d = new Date();Number(d)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 返回 1404568027739"></button></div>
</div>


<p>日期方法 <strong>getTime()</strong>   也有相同的效果。</p>

<div class="example">
<div class="example_code">
d = <span class="highELE">new</span> Date();<br>d.getTime()&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; <span class="highCOM">// 返回 1404568027739
</span><button class="copy-code-button" type="button" data-clipboard-text="d = new Date();d.getTime()&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // 返回 1404568027739"></button></div>
</div>
<br>

<hr>

<h2>自动转换类型</h2>
<p>当 JavaScript 尝试操作一个 "错误" 的数据类型时，会自动转换为 "正确" 的数据类型。</p>
<p>以下输出结果不是你所期望的：</p>

<div class="example">
<div class="example_code">
	<span class="highVAL">5</span> + <span class="highVAL">null</span>&nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 null 转换为 0<br></span><span class="highVAL">"5"</span> + <span class="highVAL">null</span>&nbsp; <span class="highCOM">// 返回"5null"&nbsp;&nbsp; 
	 null 转换为 "null"<br></span>
	<span class="highVAL">"5"</span> + <span class="highVAL">1</span> &nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 "51"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 1 转换为 "1"&nbsp; <br></span>
	<span class="highVAL">"5"</span> - <span class="highVAL">1</span>&nbsp;&nbsp;&nbsp;&nbsp; <span class="highCOM">// 返回 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 "5" 转换为 5</span><button class="copy-code-button" type="button" data-clipboard-text="5 + null&nbsp;&nbsp;&nbsp; // 返回 5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 null 转换为 0&quot;5&quot; + null&nbsp; // 返回&quot;5null&quot;&nbsp;&nbsp; 
	 null 转换为 &quot;null&quot;
	&quot;5&quot; + 1 &nbsp;&nbsp;&nbsp; // 返回 &quot;51&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 1 转换为 &quot;1&quot;&nbsp; 
	&quot;5&quot; - 1&nbsp;&nbsp;&nbsp;&nbsp; // 返回 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 &quot;5&quot; 转换为 5"></button></div>
</div>
<br>

<hr>

<h2>自动转换为字符串</h2>
<p>当你尝试输出一个对象或一个变量时 JavaScript 会自动调用变量的 toString() 方法：</p>

<div class="example">
<div class="example_code">
document.getElementById(<span class="highVAL">"demo"</span>).innerHTML = myVar;<br><br>
<span class="highCOM">myVar = {name:"Fjohn"}&nbsp; // toString 转换为 "[object Object]"<br></span>
<span class="highCOM">myVar = [1,2,3,4]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // toString 转换为 "1,2,3,4"<br></span>
<span class="highCOM">myVar = new Date()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // toString 转换为 "Fri Jul 18 2014 09:08:55 GMT+0200"<br></span>
<button class="copy-code-button" type="button" data-clipboard-text="document.getElementById(&quot;demo&quot;).innerHTML = myVar;
myVar = {name:&quot;Fjohn&quot;}&nbsp; // toString 转换为 &quot;[object Object]&quot;
myVar = [1,2,3,4]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // toString 转换为 &quot;1,2,3,4&quot;
myVar = new Date()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // toString 转换为 &quot;Fri Jul 18 2014 09:08:55 GMT+0200&quot;"></button></div>
</div>

<p>数字和布尔值也经常相互转换：</p>

<div class="example">
	<div class="example_code">
<span class="highCOM">myVar = 123&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // toString 转换为 "123"<br></span>
<span class="highCOM">myVar = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // toString 转换为 "true"<br></span>
<span class="highCOM">myVar = false&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // toString 转换为 "false"
</span><button class="copy-code-button" type="button" data-clipboard-text="myVar = 123&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // toString 转换为 &quot;123&quot;
myVar = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // toString 转换为 &quot;true&quot;
myVar = false&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // toString 转换为 &quot;false&quot;"></button></div>
</div>
 <p>下表展示了使用不同的数值转换为数字(Number), 字符串(String), 布尔值(Boolean):</p>
<table class="reference">
 <tbody><tr>
  <th>原始值</th>
  <th>转换为数字</th>
  <th>转换为字符串</th>
  <th>转换为布尔值</th>
  <th>实例</th>
 </tr>
 <tr>
  <td>false</td>
  <td>0</td>
  <td>"false"</td>
  <td>false</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_false" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>true</td>
  <td>1</td>
  <td>"true"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_true" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>0</td>
  <td>0</td>
  <td>"0"</td>
  <td>false</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_number_0" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>1</td>
  <td>1</td>
  <td>"1"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_number_1" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>"0"</td>
  <td>0</td>
  <td>"0"</td>
  <td class="important">true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_string_0" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>"000"</td>
  <td>0</td>
  <td>"000"</td>
  <td class="important">true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_string_000" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>"1"</td>
  <td>1</td>
  <td>"1"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_string_1" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>NaN</td>
  <td>NaN</td>
  <td>"NaN"</td>
  <td>false</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_nan" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>Infinity</td>
  <td>Infinity</td>
  <td>"Infinity"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_infinity" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>-Infinity</td>
  <td>-Infinity</td>
  <td>"-Infinity"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_infinity_minus" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>""</td>
  <td class="important">0</td>
  <td>""</td>
  <td class="important">false</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_string_empty" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>"20"</td>
  <td>20</td>
  <td>"20"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_string_number" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>"Runoob"</td>
  <td>NaN</td>
  <td>"Runoob"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_string_text" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>[ ]</td>
  <td class="important">0</td>
  <td>""</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_array_empty" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>[20]</td>
  <td class="important">20</td>
  <td>"20"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_array_one_number" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>[10,20]</td>
  <td>NaN</td>
  <td>"10,20"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_array_two_numbers" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>["Runoob"]</td>
  <td>NaN</td>
  <td>"Runoob"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_array_one_string" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>["Runoob","Google"]</td>
  <td>NaN</td>
  <td>"Runoob,Google"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_array_two_strings" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>function(){}</td>
  <td>NaN</td>
  <td>"function(){}"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_function" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>{ }</td>
  <td>NaN</td>
  <td>"[object Object]"</td>
  <td>true</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_object" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>null</td>
  <td class="important">0</td>
  <td>"null"</td>
  <td>false</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_null" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
 <tr>
  <td>undefined</td>
  <td>NaN</td>
  <td>"undefined"</td>
  <td>false</td>
  <td><a class="tryitbtn" href="/try/try.php?filename=tryjs_type_convert_undefined" target="_blank" rel="noopener noreferrer">尝试一下 »</a></td>
 </tr>
</tbody></table>			<!-- 其他扩展 -->
						
			</div>
			
		</div>